﻿

var _CurrentData = {};
var _pageHander = new PageHander();
var _PriceData = null;

$(function () {

    _pageHander.PageSize = 6;
    _pageHander.IsShowFloor = false;
    _CurrentData.DMY = 0;

    $("#ctrOpenFilterBreed").click(ctrOpenFilterBreed_Click);
    $("#ctrCloseFilterBreed").click(ctrOpenFilterBreed_Click);
    Highcharts.setOptions({
        lang: {
            rangeSelectorFrom: '从',
            rangeSelectorTo: '到',
            rangeSelectorZoom: '范围'
        }
    });
})

Number.prototype.GetFormatData = function (isPercent) {

    if (this == 0) {
        return "-";
    }
     
    var result = this.toFixed(2);

    if (result == 0) {
        return "-";
    }

    if (isPercent) {
        result += "%";
    }

    return result;

}

function ctrOpenFilterBreed_Click() {
    var state = $("#filterBreed").css("display");

    if (state == "none") {
        $("#filterBreed").show();
    } else {
        $("#filterBreed").hide();
    }
    
}

function DmyClick(dmy, obj) {
    $(obj).siblings().attr("class", "");
    $(obj).attr("class", "now");

    _CurrentData.DMY = dmy;
    _pageHander.PageNo = 1;
    ShowPriceImg();
}

function ReSetChart(chartName, IsSingleYC, PriceID, ResourceID, obj) {

    $("#divMCodexSubLetters").hide();

    _CurrentData.IsSingleYC = IsSingleYC;

    if (IsSingleYC) {
        _CurrentData.ResourceID = PriceID;
    }
    else {
        _CurrentData.ResourceID = ResourceID;
        _CurrentData.FilerID = ResourceID;
        GetMCodexDefaultByIndexID();

        if (obj != undefined && obj != null) {
            $(obj).parent().siblings().attr("class", "");
            $(obj).parent().attr("class", "now");
        }
    }

    _CurrentData.PriceID = PriceID;
    _CurrentData.Name = chartName;

    $("#chartName").html(chartName);
    $("#chartNamePrice").html(chartName + "价格指数图");
    $("#chartNameResource").html(chartName + "资源指数图");
    

    ShowPriceImg();
    ShowResourceImg();
    GetIndexSummary();
    $("#filterBreed").hide();

}

function GetIndexSummary() {
    //MID:99010001,IsSingleYC:false,CataID:91010001
    ZYCLib.Web.JsonAjax.MvcRequest("/IndexChannel/GetIndexSummary", { MID: _CurrentData.PriceID, IsSingleYC: _CurrentData.IsSingleYC, CataID: _CurrentData.FilerID }, "post", true, false, function (ret) {


        FormatData($("#priceDataToday"), ret.PriceData, false, true);
        FormatData($("#priceHBDataToday"), ret.PriceHBData, false, true);
        FormatData($("#priceHBRateToday"), ret.PriceHBRate, true, true);
        $("#priceDate").html(ret.PriceDate);

        FormatData($("#priceMaxdata30"), ret.PriceMaxData30, false, false);
        FormatData($("#priceMindata30"), ret.PriceMinData30, false, false);
        FormatData($("#priceMaxData180"), ret.PriceMaxData180, false, false);
        FormatData($("#priceMinData180"), ret.PriceMinData180, false, false);
        FormatData($("#priceMaxdata365"), ret.PriceMaxData365, false, false);
        FormatData($("#priceMindata365"), ret.PriceMinData365, false, false);

        FormatData($("#resourceData"), ret.ResourceData, false, true);
        FormatData($("#resourceHBData"), ret.ResourceHBData, false, true);
        FormatData($("#resourceHBRate"), ret.ResourceHBRate, true, true);
        $("#resourceYear").html(ret.ResourceYear);


    })
}

function FormatData(obj, data, isPercent, isShowMark) {

    var cssName = "";

    var result = data.toFixed(2);

    if (data == 0) {
        result = "-";
    }
    else if (data > 0) {

        if (isShowMark) {
            result = "+" + result;
        }

        cssName = "red";
    } else {

        cssName = "green";
    }

    if (isPercent && data != "-") {
        result += " %";
    }



    $(obj).html(result);

    if (isShowMark) {
        $(obj).attr("class", cssName)
    }

}

function GetMCodexDefaultByIndexID() {

    ZYCLib.Web.JsonAjax.MvcRequest("/IndexChannel/GetTopNIndexMCodexViewModelList", { IndexID: _CurrentData.FilerID, TopCount: 28 }, "post", true, false, function (ret) {

        var htmlStr = "";

        for (var i = 0; i < ret.length; i++) {
            var obj = ret[i];
            htmlStr += "  <a href=\"javascript:void(0);\" onclick=\"ReSetChart( '" + obj.FullName + "', true, " + obj.MID + " )\">" + obj.FullName + "</a>  ";
        }

        $("#divPinyinMCodex").html(htmlStr);

    })

}

function GetSubLetterForIndex(obj, letter) {

    $(obj).siblings().removeAttr("class");
    $(obj).attr("class", "selected");

    $("#divMCodexSubLetters").show();
    $("#divMCodexSubLetters").html("数据加载中……");

    ZYCLib.Web.JsonAjax.MvcRequest("/IndexChannel/GetIndexMCodexSubPinyinByLetterAndIndexID", { IndexID: _CurrentData.FilerID, Letter: letter }, "post", true, false, function (ret) {

        var htmlStr = "";

        for (var i = 0; i < ret.length; i++) {
            var obj = ret[i];
            htmlStr += " <a href=\"javascript:void(0);\" onclick=\"GetCodexNameByLetterForIndex('" + obj + "');\">" + obj + "</a>   ";
        }

        $("#divMCodexSubLetters").html(htmlStr);

    })

    GetCodexNameByLetterForIndex(letter);


 
}

function GetCodexNameByLetterForIndex(letter) {
    $("divPinyinMCodex").html("数据加载中……");

    ZYCLib.Web.JsonAjax.MvcRequest("/IndexChannel/GetIndexMCodexViewModelListByLetter", { IndexID: _CurrentData.FilerID, Letter: letter }, "post", true, false, function (ret) {

        var htmlStr = "";

        for (var i = 0; i < ret.length; i++) {
            var obj = ret[i];
            htmlStr += "<a href=\"javascript:void(0);\" onclick=\"ReSetChart( '" + obj.FullName + "', true, " + obj.MID + " )\">" + obj.FullName + "</a>   ";
        }

        $("#divPinyinMCodex").html(htmlStr);


    })


}


function GetRangeButtons() {

    var index = 1;
    var rangebutton = [];

    if (_CurrentData.DMY == "0") {//日指数
        index = 2;
        rangebutton = [
            { type: 'month', count: 1, text: '1月' },
            { type: 'month', count: 3, text: '3月' },
            { type: 'month', count: 6, text: '6月' },
            { type: 'all', text: '全部' }
        ];
       
    } else if (_CurrentData.DMY == "1") {//月指数
        rangebutton = [
            { type: 'month', count: 6, text: '6月' },
            { type: 'year', count: 1, text: '1年' },
            { type: 'year', count: 3, text: '3年' },
            { type: 'all', text: '全部' }
        ];
    } else if (_CurrentData.DMY == "2") {//年指数
    }

    return {
        selected: index,
        inputDateFormat: "%Y-%m-%d",
        buttons: rangebutton
    };
}

function FindIndex(arrData, number, startPos, endPos) {

    if (startPos == endPos) return startPos;
    var min = arrData[startPos][0];
    var max = arrData[endPos][0];
    if (min >= max) return -1;
    var midIdx = startPos + parseInt((endPos - startPos) / 2);
    var mid = arrData[midIdx][0];

    if (number == mid) return midIdx;
    if (number == min) return startPos;
    if (number == max) return endPos;

    if (number > min && number < mid)
        return FindIndex(arrData, number, startPos, midIdx);
    if (number > mid && number < max)
        return FindIndex(arrData, number, midIdx, endPos);

    return -1;
};

function ShowPriceImg() {

    ZYCLib.Web.JsonAjax.MvcRequest("/IndexChannel/GetIndexInfo", { IsSingleYC: _CurrentData.IsSingleYC, DmyType: _CurrentData.DMY, MID: _CurrentData.PriceID }, "post", true, false, function (ret) {

        var jsonData = JSON.parse(ret.IndexData);
        _PriceData = jsonData;

        $('#priceChartContainer').highcharts('StockChart', {

            rangeSelector: GetRangeButtons(),
            title: {
                text: ''
            },

            xAxis: {
                labels: {
                    format: "{value:%y-%m-%d}"
                }
            },
            series: [{
                type: "spline",
                name: '指数',
                data: jsonData,
                tooltip: {
                    valueDecimals: 2
                }
            }],
            navigator: {
                enabled: _CurrentData.DMY != 2,
                xAxis: {
                    labels: {
                        format: "{value:%Y-%m-%d}"
                    }
                }
            },
            tooltip: {
                crosshairs: true,
                shared: true,
                formatter: function () {

                    var index = FindIndex(jsonData, this.x, 0, jsonData.length - 1);

                    var unitName = "";

                    if (_CurrentData.DMY == 0) unitName = "昨日";
                    if (_CurrentData.DMY == 1) unitName = "上月";
                    if (_CurrentData.DMY == 2) unitName = "去年";

                    //2 HBData,3 HBRate,4 TBData,5TBRate
                    var htmlStr = "";

                    htmlStr += '<table width="168"><tr><td width="78">日期:</td><td width="80">';
                    htmlStr += Highcharts.dateFormat("%Y-%m-%d", this.x);
                    htmlStr += '</td></tr><tr><td>指数:</td><td>';
                    htmlStr += this.y.GetFormatData();
                    htmlStr += '</td></tr><tr><td>较';
                    htmlStr += unitName;
                    htmlStr += '幅度:</td><td>';
                    htmlStr += jsonData[index][3].GetFormatData(true);
                    htmlStr += '</td></tr>';

                    if (_CurrentData.DMY != 2) {
                        htmlStr += '<tr><td>去年同期幅度:</td><td>';
                        htmlStr += jsonData[index][5].GetFormatData(true);
                        htmlStr += '</td></tr>';
                    }

                    htmlStr += '</table>';

                    return htmlStr;

                },
                useHTML: true
            }, credits: {
                enabled: false
            }



        });


        PriceTable();


    })

   

}

function PriceTable() {
    var htmlStr = "";

    var skipCount = _pageHander.PageSize * (_pageHander.PageNo - 1);

    var beginIndex = _PriceData.length - 1 - skipCount;
    var endIndex = beginIndex - _pageHander.PageSize;

    for (var i = beginIndex; i > endIndex && i >= 0; i--) {

        htmlStr += "<li";

        if (i % 2 == 0) {
            htmlStr += " class=\"cq\"";
        }

        htmlStr += ">";

        htmlStr += "<span class=\"w5\">";
        htmlStr += Highcharts.dateFormat("%Y-%m-%d", _PriceData[i][0]);
        htmlStr += "</span>";

        htmlStr += "<span class=\"w5\">";
        htmlStr += _CurrentData.Name;
        htmlStr += "</span>";

        htmlStr += "<span class=\"w5\">";
        htmlStr += _PriceData[i][1].GetFormatData();
        htmlStr += "</span>";

        htmlStr += "<span class=\"w5\"><em class=\"";

        if (_PriceData[i][3] < 0) {
            htmlStr += "down";
        }
        else {
            htmlStr += "up";
        }

        htmlStr += "\">";
        htmlStr += _PriceData[i][3].GetFormatData(true);
        htmlStr += "</em></span>";

        if (_CurrentData.DMY != 2) {
            htmlStr += "<span class=\"w5\"><em class=\"";

            if (_PriceData[i][5] < 0) {
                htmlStr += "down";
            }
            else {
                htmlStr += "up";
            }

            htmlStr += "\">";
            htmlStr += _PriceData[i][5].GetFormatData(true);
            htmlStr += "</em></span>";
        }

        htmlStr += "</li>";

    }

    $("#PriceDataContainer").html(htmlStr);

    if (_CurrentData.DMY == 2) {
        $("#w5").hide();
    } else {
        $("#w5").show();
    }



    _pageHander.ShowPage("_pageHander", "PricePageContainer", PriceTable, _PriceData.length);
}



function ShowResourceImg() {

    ZYCLib.Web.JsonAjax.MvcRequest("/IndexChannel/GetResourceInfo", { CataID: _CurrentData.FilerID, MID: _CurrentData.ResourceID }, "post", true, false, function (ret) {

        var jsonData = JSON.parse(ret.IndexData);

        var lineData = [];

        for (var i = 0; i < jsonData.Data.length; i++) {
            lineData.push(80);
        }

        $("#ResourceDataContainer").html(ResourceTable(jsonData, 6));

        $('#resourceChartContainer').highcharts({
            chart: {
                type: 'Combination chart'
            },
            title: {
                text: ''
            },
            subtitle: {
                text: ''
            },
            xAxis: {
                categories: jsonData.Years
            },
            yAxis: {
                min: 0,
                title: {
                    text: ''
                }
            },
            tooltip: {
                enabled: true,
                shared: true,
                useHTML: true,
                formatter: function () {

                    return '<table width="122"><tr><td style="width:66px">年份:</td><td style="width:47px">' + this.x + '</td></tr>'
                        + '<tr><td>指数</td><td>' + this.y.GetFormatData() + '</td></tr>'
                        + '<tr><td>预警线</td><td>80</td></tr>'
                        + '<tr><td>较上次数值:</td><td>' + jsonData.HBData[this.points[0].point.x].GetFormatData() + '</td></tr>'
                        + '<tr><td>较上次幅度:</td><td>' + jsonData.HBRate[this.points[0].point.x].GetFormatData(true) + '</td></tr></table>';
                }
            },
            plotOptions: {
                series: {
                    marker: {
                        enabled: false
                }
            },
            },
            series: [{
                type: 'column',
                name: '',
                data: jsonData.Data,
                color: 'rgb(204,0,51)',
                },{
                    type: 'spline',
                    name: '预警线',
                    data: lineData,
                    color: '#FF7F00',
                    marker: {
                        enabled: false,
                },
                    dashStyle: 'longDash',
                    lineWidth: 1,
                    //tooltip: {
                    //    pointFormat: "预警线:{point.y}"
                    //}
                }
            ],
            legend: {
                align: "left",
                enabled: false
            },
            credits: {
                enabled: false
            },
        });
    });
};


function ResourceTable(resData, number) {

    var htmlStr = "";
    var data = resData.Data;

    for (var i = data.length - 1; i > -1; i--) {

        htmlStr += "<li";

        if (i % 2 == 0) {
            htmlStr += " class=\"cq\"";
        }

        htmlStr += ">";

        htmlStr += "<span class=\"w5\">";
        htmlStr += resData.Years[i];
        htmlStr += "</span>";

        htmlStr += "<span class=\"w5\">";
        htmlStr += _CurrentData.Name;
        htmlStr += "</span>";

        htmlStr += "<span class=\"w5\">";
        htmlStr += data[i];
        htmlStr += "</span>";

        htmlStr += "<span class=\"w5\"><em class=\"";

        if (resData.HBRate[i] < 0) {
            htmlStr += "down";
        }
        else {
            htmlStr += "up";
        }

        htmlStr += "\">";
        htmlStr += resData.HBRate[i].GetFormatData(true);
        htmlStr += "</em></span>";

        htmlStr += "</li>";

        number--;

        if (number <= 0) { break; }

    }


    return htmlStr


}
